

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="前言  没有前言，直接开整。 推导式家族 列表推导式 字典推导式 生成器推导式    其中列表推导式使用范围最广、次数最多，本文章也是主要围绕着这个知识点展开。 基础语法简单的例子1234567891011121314151617181920212223initList &#x3D; [    &amp;#123;        &amp;#x27;name&amp;#x27;: &amp;#x27;木舟1&amp;#x27;,        &amp;">
<meta property="og:type" content="article">
<meta property="og:title" content="推导式 -- 更高效地生成数据">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2022/07/21/python/advanced/%E6%8E%A8%E5%AF%BC%E5%BC%8F/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="前言  没有前言，直接开整。 推导式家族 列表推导式 字典推导式 生成器推导式    其中列表推导式使用范围最广、次数最多，本文章也是主要围绕着这个知识点展开。 基础语法简单的例子1234567891011121314151617181920212223initList &#x3D; [    &amp;#123;        &amp;#x27;name&amp;#x27;: &amp;#x27;木舟1&amp;#x27;,        &amp;">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-21T13:29:39.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.264Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="Python进阶">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>推导式 -- 更高效地生成数据 - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="推导式 -- 更高效地生成数据"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-21 21:29" pubdate>
          2022年7月21日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          5.7k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">推导式 -- 更高效地生成数据</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>  没有前言，直接开整。</p>
<h2 id="推导式家族"><a href="#推导式家族" class="headerlink" title="推导式家族"></a>推导式家族</h2><ul>
<li>列表推导式</li>
<li>字典推导式</li>
<li>生成器推导式</li>
</ul>
<p>  其中列表推导式使用范围最广、次数最多，本文章也是主要围绕着这个知识点展开。</p>
<h2 id="基础语法"><a href="#基础语法" class="headerlink" title="基础语法"></a>基础语法</h2><h3 id="简单的例子"><a href="#简单的例子" class="headerlink" title="简单的例子"></a>简单的例子</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs python">initList = [<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟1&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-string">&#x27;25&#x27;</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟2&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-string">&#x27;26&#x27;</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟3&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-string">&#x27;27&#x27;</span><br>    &#125;,<br>]<br><br>newList = [x[<span class="hljs-string">&#x27;name&#x27;</span>] <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> initList]  <span class="hljs-comment">#  [var for var in iterList]</span><br><br><span class="hljs-built_in">print</span>(newList)<br><br><span class="hljs-comment"># 运行结果</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">[&#x27;木舟1&#x27;, &#x27;木舟2&#x27;, &#x27;木舟3&#x27;]</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>
<p>上述代码很直观，就是把原来列表中的姓名取出来生成新的列表。<br>我们可以来看下常规的生成方式：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs python">initList = [<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟1&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-string">&#x27;25&#x27;</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟2&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-string">&#x27;26&#x27;</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟3&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-string">&#x27;27&#x27;</span><br>    &#125;,<br>]<br><br>newList = []<br><br><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> initList:<br>    newList.append(x[<span class="hljs-string">&#x27;name&#x27;</span>])<br><br><span class="hljs-built_in">print</span>(newList)<br><br><span class="hljs-comment"># 运行结果</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">[&#x27;木舟1&#x27;, &#x27;木舟2&#x27;, &#x27;木舟3&#x27;]</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>
<p>可以看出，在不涉及复杂算法的场景下，使用列表推导式，会使你的代码更加简洁，可读性更高。</p>
<h3 id="嵌入条件判断"><a href="#嵌入条件判断" class="headerlink" title="嵌入条件判断"></a>嵌入条件判断</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs python">initList = [<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟1&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">25</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟2&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">26</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟3&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">27</span><br>    &#125;,<br>]<br><br>newList = [x[<span class="hljs-string">&#x27;name&#x27;</span>] <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> initList <span class="hljs-keyword">if</span> x[<span class="hljs-string">&#x27;age&#x27;</span>] &gt; <span class="hljs-number">25</span>]<br><br><span class="hljs-built_in">print</span>(newList)<br><br><span class="hljs-comment"># 运行结果</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">[&#x27;木舟2&#x27;, &#x27;木舟3&#x27;]</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>
<p>上述例子嵌入了条件判断，可以覆盖更复杂的场景。最后将age大于25的人员放进了新的列表。<br>细心的同学是不是发现了，条件判断也能写成一行？<br>这个就是<code>三元表达式</code>，这种语法极高的提高了代码编写速率，作者会放在文末进行简单讲解。</p>
<h2 id="为什么要使用推导式？"><a href="#为什么要使用推导式？" class="headerlink" title="为什么要使用推导式？"></a>为什么要使用推导式？</h2><p>  为什么要使用推导式呢？<br>  这里就要和文章的标题结合起来了，<strong>更高效地生成数据</strong>。<br>  我们可以通过<code>dis</code>来解析函数执行过程，下面看示例：</p>
<h3 id="示例一：传统方式"><a href="#示例一：传统方式" class="headerlink" title="示例一：传统方式"></a>示例一：传统方式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">from</span> dis <span class="hljs-keyword">import</span> dis<br><br>initList = [<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟1&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">25</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟2&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">26</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟3&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">27</span><br>    &#125;,<br>]<br><br><span class="hljs-keyword">def</span> <span class="hljs-title function_">func</span>():<br>    lst = []<br><br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> initList:<br>        lst.append(i[<span class="hljs-string">&#x27;name&#x27;</span>])<br><br>dis(func)<br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 运行结果</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string"> 19           0 BUILD_LIST               0</span><br><span class="hljs-string">              2 STORE_FAST               0 (lst)</span><br><span class="hljs-string"></span><br><span class="hljs-string"> 21           4 SETUP_LOOP              26 (to 32)</span><br><span class="hljs-string">              6 LOAD_GLOBAL              0 (initList)</span><br><span class="hljs-string">              8 GET_ITER</span><br><span class="hljs-string">        &gt;&gt;   10 FOR_ITER                18 (to 30)</span><br><span class="hljs-string">             12 STORE_FAST               1 (i)</span><br><span class="hljs-string"></span><br><span class="hljs-string"> 22          14 LOAD_FAST                0 (lst)</span><br><span class="hljs-string">             16 LOAD_METHOD              1 (append)  # 重点</span><br><span class="hljs-string">             18 LOAD_FAST                1 (i)</span><br><span class="hljs-string">             20 LOAD_CONST               1 (&#x27;name&#x27;)</span><br><span class="hljs-string">             22 BINARY_SUBSCR</span><br><span class="hljs-string">             24 CALL_METHOD              1           # 重点</span><br><span class="hljs-string">             26 POP_TOP</span><br><span class="hljs-string">             28 JUMP_ABSOLUTE           10</span><br><span class="hljs-string">        &gt;&gt;   30 POP_BLOCK</span><br><span class="hljs-string">        &gt;&gt;   32 LOAD_CONST               0 (None)</span><br><span class="hljs-string">             34 RETURN_VALUE</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<h3 id="示例二：推导式"><a href="#示例二：推导式" class="headerlink" title="示例二：推导式"></a>示例二：推导式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">from</span> dis <span class="hljs-keyword">import</span> dis<br><br>initList = [<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟1&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">25</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟2&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">26</span><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟3&#x27;</span>,<br>        <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">27</span><br>    &#125;,<br>]<br><br><span class="hljs-keyword">def</span> <span class="hljs-title function_">func</span>():<br>    lst = [i[<span class="hljs-string">&#x27;name&#x27;</span>] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> initList]<br><br>dis(func)<br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 运行结果</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string"> 19           0 LOAD_CONST               1 </span><br><span class="hljs-string">              2 LOAD_CONST               2 (&#x27;func.&lt;locals&gt;.&lt;listcomp&gt;&#x27;)</span><br><span class="hljs-string">              4 MAKE_FUNCTION            0</span><br><span class="hljs-string">              6 LOAD_GLOBAL              0 (initList)</span><br><span class="hljs-string">              8 GET_ITER</span><br><span class="hljs-string">             10 CALL_FUNCTION            1</span><br><span class="hljs-string">             12 STORE_FAST               0 (lst)</span><br><span class="hljs-string">             14 LOAD_CONST               0 (None)</span><br><span class="hljs-string">             16 RETURN_VALUE</span><br><span class="hljs-string"> 19           0 BUILD_LIST               0</span><br><span class="hljs-string">              2 LOAD_FAST                0 (.0)</span><br><span class="hljs-string">        &gt;&gt;    4 FOR_ITER                12 (to 18)</span><br><span class="hljs-string">              6 STORE_FAST               1 (i)</span><br><span class="hljs-string">              8 LOAD_FAST                1 (i)</span><br><span class="hljs-string">             10 LOAD_CONST               0 (&#x27;name&#x27;)</span><br><span class="hljs-string">             12 BINARY_SUBSCR</span><br><span class="hljs-string">             14 LIST_APPEND              2</span><br><span class="hljs-string">             16 JUMP_ABSOLUTE            4</span><br><span class="hljs-string">        &gt;&gt;   18 RETURN_VALUE</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<p>通过两个步骤的对比，可以发现，示例二中没有真正耗时的步骤:</p>
<ul>
<li>LOAD_METHOD</li>
<li>CALL_METHOD</li>
</ul>
<p>如果涉及到的运算数据达到一定量级，传统的方式将会影响代码执行速度。</p>
<h2 id="嵌套推导式"><a href="#嵌套推导式" class="headerlink" title="嵌套推导式"></a>嵌套推导式</h2><p>既然for循环支持嵌套，那么推导式同样也是支持的。<br>不过语法表现形式在对其不熟悉的情况下，会有些难懂。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">from</span> dis <span class="hljs-keyword">import</span> dis<br><br>initList = [<br>    [<br>        &#123;<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟1&#x27;</span>,<br>            <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">25</span><br>        &#125;,<br>        &#123;<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟2&#x27;</span>,<br>            <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">26</span><br>        &#125;,<br>        &#123;<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟3&#x27;</span>,<br>            <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">27</span><br>        &#125;,<br><br>    ],<br>    [<br>        &#123;<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟4&#x27;</span>,<br>            <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">28</span><br>        &#125;,<br>        &#123;<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟5&#x27;</span>,<br>            <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">29</span><br>        &#125;,<br>        &#123;<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟6&#x27;</span>,<br>            <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">30</span><br>        &#125;,<br><br>    ],<br>    [<br>        &#123;<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;木舟7&#x27;</span>,<br>            <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">31</span><br>        &#125;,<br>    ],<br>]<br><br>newList = [j <span class="hljs-keyword">for</span> idx, innerList <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(initList) <span class="hljs-keyword">if</span> idx + <span class="hljs-number">1</span> != <span class="hljs-built_in">len</span>(initList) <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> innerList <span class="hljs-keyword">if</span> j[<span class="hljs-string">&#x27;age&#x27;</span>] &gt; <span class="hljs-number">25</span> <span class="hljs-keyword">and</span> j[<span class="hljs-string">&#x27;age&#x27;</span>] != <span class="hljs-number">30</span>]<br><br><span class="hljs-built_in">print</span>(newList)<br><br><span class="hljs-comment"># 运行结果</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">[&#123;&#x27;name&#x27;: &#x27;木舟2&#x27;, &#x27;age&#x27;: 26&#125;, &#123;&#x27;name&#x27;: &#x27;木舟3&#x27;, &#x27;age&#x27;: 27&#125;, &#123;&#x27;name&#x27;: &#x27;木舟4&#x27;, &#x27;age&#x27;: 28&#125;, &#123;&#x27;name&#x27;: &#x27;木舟5&#x27;, &#x27;age&#x27;: 29&#125;]</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>
<p>怎么样，是不是很绕哈哈。<br>其实拆分下来，还是很好理解的，首先我们把他拆分成传统方式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 为了省略篇幅，initList就忽略了，运行时可以copy之前的例子</span><br>newList = []<br><br><span class="hljs-keyword">for</span> idx, innerList <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(initList):<br>    <span class="hljs-keyword">if</span> idx + <span class="hljs-number">1</span> != <span class="hljs-built_in">len</span>(initList):<br>        <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> innerList:<br>            <span class="hljs-keyword">if</span> j[<span class="hljs-string">&#x27;age&#x27;</span>] &gt; <span class="hljs-number">25</span> <span class="hljs-keyword">and</span> j[<span class="hljs-string">&#x27;age&#x27;</span>] != <span class="hljs-number">30</span>:<br>                newList.append(j)<br></code></pre></td></tr></table></figure>
<p>解读一下代码：</p>
<ul>
<li>建立循环，通过enumerate获取元素以及元素对应的索引。不知道enumerate这个函数的，可以百度一下。</li>
<li>判断索引，通过索引+1来判断是否为initList的最后元素，不是最后一位元素才开始第二个循环</li>
<li>开始第二个循环，判断年纪超过25且不等于30，合格就放入新的数组。</li>
</ul>
<p>解析下来，清晰明了。那如果我们要读列表推导式呢？同样也可以拆分</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python">newList = [<br>    j  <span class="hljs-comment"># 最后执行层</span><br>    <span class="hljs-comment"># 运行层，由上到下依次运行。执行层为最后一步</span><br>    <span class="hljs-keyword">for</span> idx, innerList <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(initList)  <br>    <span class="hljs-keyword">if</span> idx + <span class="hljs-number">1</span> != <span class="hljs-built_in">len</span>(initList)<br>    <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> innerList<br>    <span class="hljs-keyword">if</span> j[<span class="hljs-string">&#x27;age&#x27;</span>] &gt; <span class="hljs-number">25</span> <span class="hljs-keyword">and</span> j[<span class="hljs-string">&#x27;age&#x27;</span>] != <span class="hljs-number">30</span><br>]<br></code></pre></td></tr></table></figure>
<p>按行解读，其实和传统的方式一样。是不是很简单？</p>
<h2 id="字典、生成器推导式"><a href="#字典、生成器推导式" class="headerlink" title="字典、生成器推导式"></a>字典、生成器推导式</h2><h3 id="基础语法-1"><a href="#基础语法-1" class="headerlink" title="基础语法"></a>基础语法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs python">initList = [<br>    [<span class="hljs-string">&#x27;age&#x27;</span>, <span class="hljs-number">20</span>],<br>    [<span class="hljs-string">&#x27;name&#x27;</span>, <span class="hljs-string">&#x27;木舟1&#x27;</span>],<br>]<br><br>newDict = &#123;<br>    i[<span class="hljs-number">0</span>]: i[<span class="hljs-number">1</span>]<br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> initList<br>&#125;<br><br><span class="hljs-built_in">print</span>(newDict)<br><br><span class="hljs-comment"># 运行结果</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">&#123;&#x27;age&#x27;: 20, &#x27;name&#x27;: &#x27;木舟1&#x27;&#125;</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><br><span class="hljs-comment"># 生成器推导式</span><br><br>newGenerator = (i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> initList)  <span class="hljs-comment"># 就是把原来列表推导式的 [] 换成 ()</span><br><span class="hljs-comment"># 关于生成器和迭代器的知识，会专门有文章进行讲解</span><br></code></pre></td></tr></table></figure>

<h2 id="三元表达式"><a href="#三元表达式" class="headerlink" title="三元表达式"></a>三元表达式</h2><h3 id="基础语法-2"><a href="#基础语法-2" class="headerlink" title="基础语法"></a>基础语法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs python">initList = [<span class="hljs-literal">True</span>, <span class="hljs-literal">False</span>]<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> initList:<br>    <span class="hljs-built_in">print</span>(<br>        <span class="hljs-string">&#x27;真的&#x27;</span> <span class="hljs-keyword">if</span> i <span class="hljs-keyword">else</span> <span class="hljs-string">&#x27;假的&#x27;</span><br>    )<br><br><span class="hljs-comment"># 语法： 实际表达式1  if 条件  else  实际表达式2</span><br><br><span class="hljs-comment"># 运行结果</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">真的</span><br><span class="hljs-string">假的</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>
<p>解读一下：</p>
<ul>
<li>先写条件判断通过的实际表达式</li>
<li>进行条件判断，如果if通过了，则实际产生值为 <strong>实际表达式1</strong></li>
<li>如果if没通过，则实际产生值为 <strong>实际表达式2</strong></li>
<li>三元表达式，<em><strong>没有elif</strong></em>，只有<em><strong>if - else</strong></em></li>
</ul>
<h2 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h2><p>推导式，尤其是列表推导式，在<strong>测开</strong>的面试中，为高频提问问题。<br>面试官提问推导式，主要是想考察面试者python熟练度。<br>如果连列表推导式都没用过，那么再深入的面试问题可能就不会拿来提问。<br>而且，回答时，尽量能以自己的理解，讲解出为什么推导式比常规方式更快。  </p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/python/" class="category-chain-item">python</a>
  
  
    <span>></span>
    
  <a href="/my-blog/categories/python/Python%E8%BF%9B%E9%98%B6/" class="category-chain-item">Python进阶</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/Python%E8%BF%9B%E9%98%B6/">#Python进阶</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>推导式 -- 更高效地生成数据</div>
      <div>https://walker_kwu.gitee.io/my-blog/2022/07/21/python/advanced/推导式/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月21日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2022/09/08/notes/html/firstHtml/" title="第一个网页">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">第一个网页</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2022/07/16/python/advanced/with/" title="with -- 优雅永不过时">
                        <span class="hidden-mobile">with -- 优雅永不过时</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
